//
//  HKNoticeListCellModel.m
//  HKTimeKitApp
//
//  Created by huzhaoke1 on 2023/5/11.
//

#import "HKNoticeListCellModel.h"
#import "HKNoticeCell.h"
#import "HKNoticeModel.h"
#import "HKRequestDefine.h"
#import "HKWebViewController.h"
#import "HKShareWebViewControllerManager.h"
#import "HTTPURL.h"
#import "HKRequestDefine.h"

@interface HKNoticeListCellModel ()

@property (nonatomic, strong) HKWebViewContainer *webViewContainer;
@property (nonatomic, strong) HKWebViewController *webViewController;

@end

@implementation HKNoticeListCellModel

- (instancetype)initWithContent:(id)content {
    if (self = [super initWithContent:content]) {
        HKNoticeModel *noticeModel = KSSAFE_CAST(content, HKNoticeModel);
//        if (![[HKShareWebViewControllerManager sharedInstance] webViewContainerWithUrl:[noticeModel currentUrl]]) {
//            [self.webViewController loadingRequestWithUrl:[noticeModel currentUrl]];
//        }
        self.cellClass = [HKNoticeCell class];
        self.content = content;
        self.cellHeight = [HKNoticeCell cellHeightWithContent:content];
        [self setRender:^(__kindof HKListCellModel * _Nonnull cellModel, id  _Nonnull cell) {
            HKBaseTableViewCell *tbCell = KSSAFE_CAST(cell, HKBaseTableViewCell);     
            [tbCell updateContent:cellModel.content];
        }];
        WS
        [self setCellDidSelectBlock:^(HKListCellModel * _Nonnull cellModel, NSIndexPath * _Nonnull indexPath, id  _Nonnull cell) {
            SS
            HKNoticeModel *model = KSSAFE_CAST(cellModel.content, HKNoticeModel);
            [self didSelectNoticeModel:model];
        }];
    }
    return self;
}

- (void)didSelectNoticeModel:(HKNoticeModel *)model {
    [self requestHasReadWith:model];
    NSString *url = model.currentUrl;
    if (url.length == 0) return;
    HKWebViewController *webViewController = [HKWebViewController new];
    HKWebViewContainer *webViewContainer;
    if (![[HKShareWebViewControllerManager sharedInstance] webViewContainerWithUrl:url]) {
        webViewContainer = self.webViewContainer;
    } else {
        webViewContainer = [[HKShareWebViewControllerManager sharedInstance] webViewContainerWithUrl:url];
    }
    [self.webViewController loadingRequestWithUrl:[model currentUrl]];
    webViewController.webViewContainer = webViewContainer;
    [self.context.fromViewController.navigationController pushViewController:webViewController animated:YES];
}

- (void)requestHasReadWith:(HKNoticeModel *)model {
    NSString *url = [NSString stringWithFormat:@"%@%@", HKHasReadUrl, @(model.modelId)];
    [HTTPURL getjsonRequest:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        [[NSNotificationCenter defaultCenter] postNotificationName:Notification_Name_MessageRefresh object:nil userInfo:nil];
    } filure:^(NSURLSessionDataTask *task, NSError *error) {
    }];
}

- (HKWebViewContainer *)webViewContainer {
    if (!_webViewContainer) {
        _webViewContainer = [HKWebViewContainer new];
    }
    return _webViewContainer;
}


- (HKWebViewController *)webViewController {
    if (!_webViewController) {
        _webViewController = [HKWebViewController new];
        _webViewController.webViewContainer = self.webViewContainer;
    }
    return _webViewController;
}

@end
